package com.nutrition.health.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "food_items")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude = {"nutritionInfo", "dietRecords"})
@EntityListeners(AuditingEntityListener.class)
public class FoodItem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    @Size(max = 100)
    private String name;

    @Size(max = 500)
    private String description;

    @Enumerated(EnumType.STRING)
    private FoodCategory category;

    @Column(length = 255)
    private String imageUrl;

    @Column(length = 50)
    private String barcode;

    // 不再使用 @OneToOne 关联，改为在 NutritionInfo 中使用 foodItemId

    @ManyToMany(mappedBy = "foodItems")
    @JsonIgnore
    private Set<DietRecord> dietRecords = new HashSet<>();

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    // 食品类别枚举
    public enum FoodCategory {
        FRUITS, // 水果
        VEGETABLES, // 蔬菜
        GRAINS, // 谷物
        PROTEINS, // 蛋白质食品
        DAIRY, // 乳制品
        FATS_AND_OILS, // 脂肪和油
        SWEETS, // 甜食
        BEVERAGES, // 饮料
        SNACKS, // 零食
        PREPARED_FOODS, // 预制食品
        OTHER // 其他
    }
}